<?php

namespace app\models;

use Yii;
use yii\db\Query;
/**
 * This is the model class for table "{{%sys_users}}".
 *
 * @property integer $id
 * @property string $phone
 * @property string $parent_phone
 * @property integer $type
 * @property string $password
 * @property integer $role
 * @property integer $last_login_time
 * @property string $truename
 * @property string $qq
 * @property string $qq_openid
 * @property string $email
 * @property string $weixin
 * @property string $wx_openid
 * @property string $tel
 * @property string $company_name
 * @property string $company_addr
 * @property string $company_regnum
 * @property string $company_range
 * @property string $company_spec_range
 * @property string $company_type
 * @property string $company_est_time
 * @property string $company_licence
 * @property integer $reg_time
 * @property integer $status
 */
class Complete extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%sys_users}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['phone', 'password'], 'required'],
            [['type', 'role', 'last_login_time', 'reg_time', 'status'], 'integer'],
            [['phone', 'parent_phone'], 'string', 'max' => 11],
            [['password'], 'string', 'max' => 120],
            [['truename', 'qq', 'tel', 'company_type'], 'string', 'max' => 20],
            [['qq_openid', 'wx_openid'], 'string', 'max' => 255],
            [['email', 'weixin'], 'string', 'max' => 50],
            [['company_name', 'company_regnum'], 'string', 'max' => 40],
            [['company_addr', 'company_licence'], 'string', 'max' => 100],
            [['company_range', 'company_spec_range', 'company_est_time'], 'string', 'max' => 500]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'phone' => 'Phone',
            'parent_phone' => 'Parent Phone',
            'type' => 'Type',
            'password' => 'Password',
            'role' => 'Role',
            'last_login_time' => 'Last Login Time',
            'truename' => 'Truename',
            'qq' => 'Qq',
            'qq_openid' => 'Qq Openid',
            'email' => 'Email',
            'weixin' => 'Weixin',
            'wx_openid' => 'Wx Openid',
            'tel' => 'Tel',
            'company_name' => 'Company Name',
            'company_addr' => 'Company Addr',
            'company_regnum' => 'Company Regnum',
            'company_range' => 'Company Range',
            'company_spec_range' => 'Company Spec Range',
            'company_type' => 'Company Type',
            'company_est_time' => 'Company Est Time',
            'company_licence' => 'Company Licence',
            'reg_time' => 'Reg Time',
            'status' => 'Status',
        ];
    }
    public function SelBalance($uid,$type)
    {
        if($type == 'bzj'){
            $member = (new Query())->select("*")->from("dh_sys_bzj_detail")->where(['uid'=>$uid])->one();
        }else if($type == 'ye'){
            $member = (new Query())->select("*")->from("dh_sys_balance")->where(['uid'=>$uid])->one();
        }else if($type == 'yhj'){
            $member = '';
        }
        return $member;
    }
   public function SelGoodsQgHy($uid,$type){
       
       if($type == 'qg'){
           $goodsnum = (new Query())->select("*")->from("dh_base_goods_qg")->where(['uid'=>$uid,'status'=>1,'flag'=>1])->count();
       }else if($type == 'hy'){
           $goodsnum = (new Query())->select("*")->from("dh_base_goods_hy")->where(['qy_id'=>$uid,'statuses'=>1,'flag'=>1])->count();
       }
       return $goodsnum;
   }
   public function SelGoodsAddress($uid,$type){
       if($type == 'sh'){
           $address = (new Query())->select("*")->from("dh_sys_address")->where(['userid'=>$uid,'type'=>1,'status'=>1,'isdefault'=>1])->all();
//                   ->orderBy('isdefault ASC')->limit(2)->all();
       }else if($type =='fh'){
           $address = (new Query())->select("*")->from("dh_sys_address")->where(['userid'=>$uid,'type'=>2,'status'=>1,'isdefault'=>1])->all();
       }
       return  $address;
   }
   
   public function SelGoodsWeiDai($uid,$type){
       if($type == 'mj'){
           $sql = "select count(*) as nums ,status from dh_order where buy_uid=".$uid." group by status";
           $goodstype = Complete::findBySql($sql)->asArray()->all();
       }else if($type == 'nj'){
           $sql = "select count(*) as nums ,status from dh_order where seller_uid=".$uid." group by status";
           $goodstype = Complete::findBySql($sql)->asArray()->all();
       }
           $tags = [];
           foreach ($goodstype as $val){
               $tags[$val['status']] = $val['nums'];
           }
//           print_r($uid);die();
           return $tags;

   }
   
   public function SelCoupon($uid){
       
       $coupon_list = (new Query())->select("*")->from("dh_coupon_list")->where(['user_id'=>$uid])->count();
       
       return $coupon_list;
//       print_r($coupon_list);die();
   }
   
   public function SelgoodsHy($uid){
       
       $num = (new Query())->select("*")->from("dh_base_goods_hy")->where(['qy_id'=>$uid])->count();
       if($num >0){
           return 1;
       }else{
           return 0;
       }
   }
   
   public function GetHyQgNum($uid,$type) {
        $year = date("Y");
        $month = date("m");
//        $allday = date("t");
        $strat_time = strtotime($year."-".$month."-1");
        $condition =  ' and a.finish_time>="' . $strat_time . '"';
//        echo $strat_time;die();
       if($type=="hy"){
            $counthynum = (new Query())->select("sum(b.quantity) as hynum,sum(a.goods_sumPrice) as sumprice")->from("dh_order as a")->
                    leftJoin("dh_order_detail as b","a.orderId=b.orderId")->where(['a.seller_uid'=>$uid,'a.status'=>4])->andWhere('1=1'.$condition)->one();
       }else{
            $counthynum = (new Query())->select("sum(b.quantity) as qgnum,sum(a.goods_sumPrice) as sumprice")->from("dh_order as a")->
                    leftJoin("dh_order_detail as b","a.orderId=b.orderId")->where(['a.buy_uid'=>$uid,'a.status'=>4])->andwhere('1=1'.$condition)->one();
       }
       return $counthynum;
//       print_r($counthynum);die();
   }
}
